/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Negocio.CajeroFactura;
import Negocio.Ciudad;
import Negocio.CiudadCliente;
import Negocio.Cliente;
import Negocio.Compras;
import Negocio.Factura;
import Negocio.FacturaCliente;
import Negocio.Lista_cc;
import Negocio.Lista_ciudades;
import Negocio.Lista_clientes;
import Negocio.Lista_compras;
import Negocio.Lista_facturas;
import Negocio.Lista_login;
import Negocio.Lista_ul;
import Negocio.Lista_usuarios;
import Negocio.Login;
import Negocio.Producto;
import Negocio.UsuarioLogin;
import Negocio.Usuarios;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge
 */
public class CajeroInterfaz extends javax.swing.JFrame {
    Validacion validar = new Validacion();
    Factura factura = new Factura();
    Cliente cliente = new Cliente();
    Producto producto = new Producto();
    Ciudad ciudad = new Ciudad();
    Compras compra = new Compras();
    Login login = new Login();
    Usuarios usuario = new Usuarios();
    UsuarioLogin ul = new UsuarioLogin();
    FacturaCliente fc = new FacturaCliente(); 
    CiudadCliente cc = new CiudadCliente();
    CajeroFactura cf = new CajeroFactura();
    Lista_ul listaUL = new Lista_ul();
    Lista_usuarios listaUsuarios = new Lista_usuarios();
    Lista_login listaLogin = new Lista_login();
    Lista_cc listaCC = new Lista_cc();
    Lista_clientes listaClientes = new Lista_clientes();
    Lista_facturas listaFacturas = new Lista_facturas();
    Lista_compras listaCompras = new Lista_compras();
    Lista_ciudades listaCiudad = new Lista_ciudades();
    DefaultComboBoxModel comboCiudad = new DefaultComboBoxModel();
    SimpleDateFormat formato;
    Date hoy = new Date();
    LoginInterfaz log;
    /**
     * Creates new form CajeroInterfaz
     */
    public CajeroInterfaz() {
        initComponents();
        this.setTitle("CAJERO");        
        formato=new SimpleDateFormat("yyyy-MM-dd");
        lblFecha.setText(formato.format(hoy));        
        lblIdFact.setText("");
        lblTotal.setText("");        
    }
    
    private void limpiarcliente(){
        txtCedulaCli.setText("");
        txtNombreCli.setText("");
        txtApellidoCli.setText("");
        txtCalleCli.setText("");
        txtNumeroCli.setText("");
        txtTelefonoCli.setText("");
        txtCedulaCli.setEnabled(true);
        txtNombreCli.setEnabled(true);
        txtApellidoCli.setEnabled(true);
    }
    
    public void limpiarTabla(JTable tbl){
        int aux = tbl.getRowCount();
        do{
            try{
                DefaultTableModel temp = (DefaultTableModel) tbl.getModel();
                temp.removeRow(temp.getRowCount()-1);
            }
            catch(ArrayIndexOutOfBoundsException e){
            }
            aux--;
        }while(aux > 0);
    }
    
    public void listarCompras(int id){
            listaCompras.listar(id);
            double precio;
            double suma=0;
            ArrayList compras = new ArrayList();
            compras = listaCompras.getCompras();
            DefaultTableModel temp = (DefaultTableModel) tblFacturaCajero.getModel();
            for (int i = tblFacturaCajero.getRowCount() - 1; i >= 0; i--) {
                temp.removeRow(i);
            }
            for (int i = 0; i < compras.size(); i++) {
                Compras otraCompra = new Compras();
                otraCompra = (Compras) compras.get(i);
                producto.setId(otraCompra.getP().getId());
                producto.buscar();
                precio=otraCompra.getP().getCantidad()*producto.getPrecio();
                suma=precio+suma;
                lblTotal.setText(suma+" $");
                Object nuevo[] = {otraCompra.getP().getId(), producto.getNombre(), otraCompra.getP().getCantidad(),  precio};
                temp.addRow(nuevo);            
            }
            tblFacturaCajero.setModel(temp);
    }
    
    private void limpiarFactura(){
        txtCedulaFact.setText("");
        txtIdProdFact.setText("");
        txtCantidadFact.setText("");
        limpiarTabla(tblFacturaCajero);
        lblTotal.setText("");
    }
    
    public void agregarCiudadesCombo() {
        comboCiudad.removeAllElements();
        listaCiudad.listar();
        ArrayList ciudades = listaCiudad.getCiudades();
        Iterator it = ciudades.iterator();
        while (it.hasNext()) {
            Ciudad c = (Ciudad) it.next();
            comboCiudad.addElement(c.getNombre());
        }
        cmbCiudades.setModel(comboCiudad);
    }

    private void actualizarcliente(){
        listaClientes.listar();
        ArrayList clientes = new ArrayList();
        clientes = listaClientes.getClientes();
        DefaultTableModel temp = (DefaultTableModel) tblClientesCajero.getModel();
        for (int i = tblClientesCajero.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
        for (int i = 0; i < clientes.size(); i++) {
            Cliente otroCliente = new Cliente();
            otroCliente = (Cliente) clientes.get(i);
            cliente.setCedula(otroCliente.getCedula());
            cc.setC(cliente);
            cc.buscar();
            ciudad.setId(cc.getCiu().getId());
            ciudad.buscar();
            if(ciudad.getNombre()==null){
                Object nuevo[] = {otroCliente.getCedula(), otroCliente.getNombre(), otroCliente.getApellido(), otroCliente.getDir_calle(), otroCliente.getDir_numero(), otroCliente.getTelefono(), "Ciudad no agregada"};
                temp.addRow(nuevo);
            }
            else{
                Object nuevo[] = {otroCliente.getCedula(), otroCliente.getNombre(), otroCliente.getApellido(), otroCliente.getDir_calle(), otroCliente.getDir_numero(), otroCliente.getTelefono(), ciudad.getNombre()};
                temp.addRow(nuevo);
            }
            
        }
        tblClientesCajero.setModel(temp);      
       }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblCedUs = new javax.swing.JLabel();
        lblNombreUs = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtCedulaCli = new javax.swing.JTextField();
        txtNombreCli = new javax.swing.JTextField();
        txtApellidoCli = new javax.swing.JTextField();
        txtCalleCli = new javax.swing.JTextField();
        txtNumeroCli = new javax.swing.JTextField();
        txtTelefonoCli = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        cmbCiudades = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblClientesCajero = new javax.swing.JTable();
        btnAgregarCli = new javax.swing.JButton();
        btnBuscarCli = new javax.swing.JButton();
        btnModificarCli = new javax.swing.JButton();
        btnActualizarCli = new javax.swing.JButton();
        btnLimpiarCli = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        lblIdFact = new javax.swing.JLabel();
        txtCedulaFact = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        txtIdProdFact = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        txtCantidadFact = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        lblFecha = new javax.swing.JLabel();
        btnCrearFact = new javax.swing.JButton();
        btnAgregarCompra = new javax.swing.JButton();
        btnEliminarFact = new javax.swing.JButton();
        btnLimpiarFact = new javax.swing.JButton();
        btnFinalizarFact = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblFacturaCajero = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        cerrarCajero = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Usuario:");

        lblCedUs.setText("0103849873");

        lblNombreUs.setText("jLabel3");

        jLabel4.setText("Cedula:");

        jLabel5.setText("Nombre:");

        jLabel6.setText("Apellido:");

        jLabel7.setText("Direccion Calle:");

        jLabel8.setText("Direccion Numero:");

        jLabel9.setText("Telefono:");

        jLabel10.setText("Ciudad:");

        cmbCiudades.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tblClientesCajero.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Cedula", "Nombre", "Apellido", "Calle", "Numero", "Telefono", "Ciudad"
            }
        ));
        jScrollPane1.setViewportView(tblClientesCajero);

        btnAgregarCli.setText("Agregar");
        btnAgregarCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarCliActionPerformed(evt);
            }
        });

        btnBuscarCli.setText("Buscar");
        btnBuscarCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarCliActionPerformed(evt);
            }
        });

        btnModificarCli.setText("Modificar");
        btnModificarCli.setEnabled(false);
        btnModificarCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarCliActionPerformed(evt);
            }
        });

        btnActualizarCli.setText("Actualizar");
        btnActualizarCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarCliActionPerformed(evt);
            }
        });

        btnLimpiarCli.setText("Limpiar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 545, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)))
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCedulaCli)
                    .addComponent(txtNombreCli)
                    .addComponent(txtApellidoCli)
                    .addComponent(txtCalleCli)
                    .addComponent(txtNumeroCli)
                    .addComponent(txtTelefonoCli)
                    .addComponent(cmbCiudades, 0, 144, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnAgregarCli, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnActualizarCli, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnModificarCli, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnBuscarCli, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLimpiarCli, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(94, 94, 94))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtCedulaCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtNombreCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtApellidoCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtCalleCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtNumeroCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtTelefonoCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(btnAgregarCli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarCli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificarCli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnActualizarCli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiarCli)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(cmbCiudades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Cliente", jPanel1);

        jLabel13.setText("Nro. Factura:");

        lblIdFact.setText("jLabel14");

        txtCedulaFact.setEnabled(false);
        txtCedulaFact.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCedulaFactKeyPressed(evt);
            }
        });

        jLabel17.setText("Cedula:");

        jLabel19.setText("Producto Id:");

        txtIdProdFact.setEnabled(false);
        txtIdProdFact.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIdProdFactKeyPressed(evt);
            }
        });

        jLabel18.setText("Cantidad:");

        txtCantidadFact.setEnabled(false);
        txtCantidadFact.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCantidadFactKeyPressed(evt);
            }
        });

        jLabel15.setText("Fecha Actual:");

        lblFecha.setText("jLabel16");

        btnCrearFact.setText("Crear");
        btnCrearFact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrearFactActionPerformed(evt);
            }
        });

        btnAgregarCompra.setText("Agregar");
        btnAgregarCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarCompraActionPerformed(evt);
            }
        });

        btnEliminarFact.setText("Eliminar");
        btnEliminarFact.setEnabled(false);
        btnEliminarFact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarFactActionPerformed(evt);
            }
        });

        btnLimpiarFact.setText("Limpiar");
        btnLimpiarFact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarFactActionPerformed(evt);
            }
        });

        btnFinalizarFact.setText("Finalizar");
        btnFinalizarFact.setEnabled(false);
        btnFinalizarFact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarFactActionPerformed(evt);
            }
        });

        tblFacturaCajero.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Producto Id", "Cantidad", "Precio"
            }
        ));
        jScrollPane4.setViewportView(tblFacturaCajero);

        jLabel2.setText("Total:");

        lblTotal.setText("jLabel3");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(333, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblTotal)
                        .addGap(237, 237, 237))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAgregarCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCrearFact, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnEliminarFact, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnLimpiarFact, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnFinalizarFact, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45))))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel13)
                            .addGap(18, 18, 18)
                            .addComponent(lblIdFact)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel15))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(17, 17, 17)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGap(99, 99, 99)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel18)
                                        .addComponent(jLabel19)
                                        .addComponent(jLabel17))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtCedulaFact)
                                        .addComponent(txtIdProdFact)
                                        .addComponent(txtCantidadFact, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 44, Short.MAX_VALUE)))
                    .addGap(18, 18, 18)
                    .addComponent(lblFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(23, 23, 23)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(btnCrearFact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAgregarCompra)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminarFact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiarFact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFinalizarFact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblTotal))
                .addGap(31, 31, 31))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(lblFecha)
                        .addComponent(jLabel13)
                        .addComponent(lblIdFact))
                    .addGap(25, 25, 25)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel17)
                        .addComponent(txtCedulaFact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(10, 10, 10)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel19)
                        .addComponent(txtIdProdFact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel18)
                        .addComponent(txtCantidadFact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(64, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Factura", jPanel2);

        jMenu1.setText("File");

        cerrarCajero.setText("Cerrar Sesion");
        cerrarCajero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cerrarCajeroActionPerformed(evt);
            }
        });
        jMenu1.add(cerrarCajero);
        jMenu1.add(jSeparator1);

        jMenuItem2.setText("Salir");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblCedUs)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNombreUs)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblCedUs)
                    .addComponent(lblNombreUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtCedulaFactKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCedulaFactKeyPressed
        // TODO add your handling code here:
        validar.ValidarNumeros(txtCedulaFact);
    }//GEN-LAST:event_txtCedulaFactKeyPressed

    private void txtIdProdFactKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdProdFactKeyPressed
        // TODO add your handling code here:
        validar.ValidarNumeros(txtIdProdFact);
    }//GEN-LAST:event_txtIdProdFactKeyPressed

    private void txtCantidadFactKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadFactKeyPressed
        // TODO add your handling code here:
        validar.ValidarNumeros(txtCantidadFact);
    }//GEN-LAST:event_txtCantidadFactKeyPressed

    private void btnCrearFactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrearFactActionPerformed
        // TODO add your handling code here:
        factura.setFecha(lblFecha.getText());
        factura.grabar();
        txtCedulaFact.setEnabled(true);
        txtIdProdFact.setEnabled(true);
        txtCantidadFact.setEnabled(true);
        lblIdFact.setText(String.valueOf(listaFacturas.getId()));
        btnCrearFact.setEnabled(false);
    }//GEN-LAST:event_btnCrearFactActionPerformed

    private void btnAgregarCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarCompraActionPerformed
        // TODO add your handling code here:
        /* int id = Integer.parseInt(lblIdFact.getText());
        factura.setFecha(lblFecha.getText());
        factura.setId(id);
        factura.grabar(); */
        int cant;
        Producto p = new Producto();
        try{
            cliente.setCedula(txtCedulaFact.getText());
            cliente.buscar();
            if(cliente.getNombre()!=null ){
                factura.setId(Integer.parseInt(lblIdFact.getText()));
                producto.setId(Integer.parseInt(txtIdProdFact.getText()));
                producto.buscar();
                p.setId(Integer.parseInt(txtIdProdFact.getText()));
                p.setCantidad(Integer.parseInt(txtCantidadFact.getText()));
                compra.setF(factura);
                compra.setP(p);
                if(producto.getCantidad()>=compra.getP().getCantidad()){
                    cant=producto.getCantidad()-compra.getP().getCantidad();
                    producto.setCantidad(cant);
                    producto.modificar();
                    compra.grabar();
                    fc.setC(cliente);
                    fc.setF(factura);
                    fc.buscar();
                    fc.grabar();
                    txtCedulaFact.setEnabled(false);
                    listarCompras(Integer.parseInt(lblIdFact.getText()));
                }
                else {
                    JOptionPane.showMessageDialog(null, "Solo existen "+producto.getCantidad()+" productos: "+producto.getNombre());
                }
            }
            txtIdProdFact.setText("");
            txtCantidadFact.setText("");
            btnEliminarFact.setEnabled(true);
            btnFinalizarFact.setEnabled(true);
        } catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Faltan ingresar datos");
        }
    }//GEN-LAST:event_btnAgregarCompraActionPerformed

    private void btnEliminarFactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarFactActionPerformed
        // TODO add your handling code here:
        Producto p = new Producto();
        int cant;
        try{
            int id = (int) tblFacturaCajero.getValueAt(tblFacturaCajero.getSelectedRow(), 0);
            int cantidad = (int) tblFacturaCajero.getValueAt(tblFacturaCajero.getSelectedRow(), 2);
            producto.setId(id);
            producto.buscar();
            p.setId(id);
            p.setCantidad(cantidad);
            compra.setP(p);
            compra.buscar();
            cant=compra.getP().getCantidad()+producto.getCantidad();
            producto.setCantidad(cant);
            producto.modificar();
            compra.eliminar();
            listarCompras(Integer.parseInt(lblIdFact.getText()));
        }catch(ArrayIndexOutOfBoundsException e){
            JOptionPane.showMessageDialog(null, "Para eliminar debe seleccionar un producto de la lista");
        }
    }//GEN-LAST:event_btnEliminarFactActionPerformed

    private void btnLimpiarFactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarFactActionPerformed
        limpiarFactura();
    }//GEN-LAST:event_btnLimpiarFactActionPerformed

    private void btnFinalizarFactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarFactActionPerformed
        // TODO add your handling code here:
        factura.setId(Integer.parseInt(lblIdFact.getText()));
        usuario.setCedula(lblCedUs.getText());
        cf.setFactura(factura);
        cf.setUsuario(usuario);
        cf.grabar();
        txtCedulaFact.setText("");
        txtIdProdFact.setText("");
        txtCantidadFact.setText("");
        txtCedulaFact.setEnabled(false);
        txtIdProdFact.setEnabled(false);
        txtCantidadFact.setEnabled(false);
        lblIdFact.setText("");
        limpiarTabla(tblFacturaCajero);
        btnCrearFact.setEnabled(true);
        btnFinalizarFact.setEnabled(false);
        lblTotal.setText("");
    }//GEN-LAST:event_btnFinalizarFactActionPerformed

    private void cerrarCajeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cerrarCajeroActionPerformed
        // TODO add your handling code here:
        log = new LoginInterfaz();
        log.setVisible(true);
        this.setVisible(false);       
    }//GEN-LAST:event_cerrarCajeroActionPerformed

    private void btnActualizarCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarCliActionPerformed
        // TODO add your handling code here:
        actualizarcliente();
    }//GEN-LAST:event_btnActualizarCliActionPerformed

    private void btnBuscarCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarCliActionPerformed
        // TODO add your handling code here:
        try{
            String cedu =  JOptionPane.showInputDialog("Ingrese una cedula");
            if(cedu.equals("")==false){
                cliente.setCedula(cedu);
                cliente.buscar();
                if (cliente.getNombre() == null) {
                    txtNombreCli.setText("");
                    txtApellidoCli.setText("");
                    txtCalleCli.setText("");
                    txtNumeroCli.setText("");
                    txtTelefonoCli.setText("");
                    btnModificarCli.setEnabled(false);
                } else {
                    btnModificarCli.setEnabled(true);
                    btnAgregarCli.setEnabled(false);

                    txtCedulaCli.setEnabled(false);
                    txtCedulaCli.setText(cliente.getCedula());
                    txtNombreCli.setText(cliente.getNombre());
                    txtApellidoCli.setText(cliente.getApellido());
                    txtNombreCli.setEnabled(false);
                    txtApellidoCli.setEnabled(false);
                    txtCalleCli.setText(cliente.getDir_calle());
                    txtNumeroCli.setText(cliente.getDir_numero());
                    txtTelefonoCli.setText(cliente.getTelefono()+"");
                }
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Operacion Cancelada");
        }
    }//GEN-LAST:event_btnBuscarCliActionPerformed

    private void btnAgregarCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarCliActionPerformed
        // TODO add your handling code here:
        cliente.setCedula(txtCedulaCli.getText());
        cliente.setNombre(txtNombreCli.getText());
        cliente.setApellido(txtApellidoCli.getText());
        cliente.setDir_calle(txtCalleCli.getText());
        cliente.setDir_numero(txtNumeroCli.getText());
        cliente.setTelefono(txtTelefonoCli.getText());
        cc.setC(cliente);
        if(comboCiudad.getSelectedItem()!=null){
            String nombre = (String) comboCiudad.getSelectedItem();
            ciudad.setNombre(nombre);
            ciudad.buscarNombre();
            cc.setCiu(ciudad);
            cliente.grabar();
            cc.grabar();
        }
        else{
            JOptionPane.showMessageDialog(null, "Primero agregue una ciudad");
        }        
    }//GEN-LAST:event_btnAgregarCliActionPerformed

    private void btnModificarCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarCliActionPerformed
        // TODO add your handling code here:
        cliente.setDir_calle(txtCalleCli.getText());
        cliente.setDir_numero(txtNumeroCli.getText());
        cliente.setTelefono(txtTelefonoCli.getText());
        String nombre = comboCiudad.getSelectedItem().toString();
        ciudad.setNombre(nombre);
        ciudad.buscarNombre();
        cliente.setCedula(txtCedulaCli.getText());
        cc.setC(cliente);
        cc.setCiu(ciudad);
        
        cc.modificar();
        cliente.modificar();
        btnModificarCli.setEnabled(false);
        txtCedulaCli.setEnabled(true);
        txtNombreCli.setEnabled(true);
        txtApellidoCli.setEnabled(true);
        btnAgregarCli.setEnabled(true);
        limpiarcliente();
    }//GEN-LAST:event_btnModificarCliActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        login.setId(listaLogin.getId());
        ul.setLogin(login);
        ul.buscarId();        
        usuario.setCedula(ul.getUsuario().getCedula());
        usuario.buscar();        
        lblCedUs.setText(usuario.getCedula());
        lblNombreUs.setText(usuario.getNombre()+" "+usuario.getApellido());
        agregarCiudadesCombo();
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CajeroInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CajeroInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CajeroInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CajeroInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CajeroInterfaz().setVisible(false);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizarCli;
    private javax.swing.JButton btnAgregarCli;
    private javax.swing.JButton btnAgregarCompra;
    private javax.swing.JButton btnBuscarCli;
    private javax.swing.JButton btnCrearFact;
    private javax.swing.JButton btnEliminarFact;
    private javax.swing.JButton btnFinalizarFact;
    private javax.swing.JButton btnLimpiarCli;
    private javax.swing.JButton btnLimpiarFact;
    private javax.swing.JButton btnModificarCli;
    private javax.swing.JMenuItem cerrarCajero;
    private javax.swing.JComboBox cmbCiudades;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblCedUs;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblIdFact;
    private javax.swing.JLabel lblNombreUs;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTable tblClientesCajero;
    private javax.swing.JTable tblFacturaCajero;
    private javax.swing.JTextField txtApellidoCli;
    private javax.swing.JTextField txtCalleCli;
    private javax.swing.JTextField txtCantidadFact;
    private javax.swing.JTextField txtCedulaCli;
    private javax.swing.JTextField txtCedulaFact;
    private javax.swing.JTextField txtIdProdFact;
    private javax.swing.JTextField txtNombreCli;
    private javax.swing.JTextField txtNumeroCli;
    private javax.swing.JTextField txtTelefonoCli;
    // End of variables declaration//GEN-END:variables
}
